package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.saas.domain.dao.CustomerRuleDao;
import com.xbongbong.saas.domain.entity.CustomerRuleEntity;
import com.xbongbong.saas.enums.CustomerRuleEnum;
import com.xbongbong.saas.model.CustomerRuleModel;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author youli.chen
 * @version v1.0
 * @ProjectName xbb-pro
 * @Description: CustomerRuleModel业务实现
 * @date 2019/1/24 11:08
 * @since v1.0
 */
@Service("customerRuleModel")
public class CustomerRuleModelImpl implements CustomerRuleModel {

    @Resource
    private CustomerRuleDao customerRuleDao;

    /**
     * 逻辑删除
     *
     * @param key    id
     * @param corpid 公司id
     * @return
     * @version v1.0
     * @since v1.0
     */
    @Override
    public Integer deleteByKey(Integer key, String corpid) {
        return customerRuleDao.deleteByKey(key, corpid);
    }

    /**
     * 获取公司指定类型的公海规则
     *
     * @param ruleType 规则类型
     * @param corpid   公司id
     * @return
     * @version v1.0
     * @since v1.0
     */
    @Override
    public CustomerRuleEntity getByRuleType(Integer ruleType, String corpid) {
        return customerRuleDao.getByRuleType(ruleType, corpid);
    }

    /**
     * 获取实体
     *
     * @param key    id
     * @param corpid 公司id
     * @return
     * @version v1.0
     * @since v1.0
     */
    @Override
    public CustomerRuleEntity getByKey(Integer key, String corpid) {
        return customerRuleDao.getByKey(key, corpid);
    }

    /**
     * 获取有设置公海规则的公司id
     *
     * @param now
     * @param startCorpid
     * @param pageNum
     * @return
     * @version v1.0
     * @since v1.0
     */
    @Override
    public List<String> findCorps(Long now, String startCorpid, Integer pageNum) {
        return customerRuleDao.findCorps(now, startCorpid, pageNum);
    }

    /**
     * 获取设置了公海规则的公司数
     *
     * @return
     * @version v1.0
     * @since v1.0
     */
    @Override
    public Integer getCorpsCount(Long now) {
        return customerRuleDao.getCorpsCount(now);
    }

    /**
     * 插入
     *
     * @param entity
     * @return
     * @throws XbbException
     * @version v1.0
     * @since v1.0
     */
    @Override
    public Integer insert(CustomerRuleEntity entity) throws XbbException {
        return customerRuleDao.insert(entity);
    }

    @Override
    public Integer update(CustomerRuleEntity entity) throws XbbException {
        return customerRuleDao.update(entity);
    }

    /**
     * 获取符合查询条件的记录
     *
     * @param param 查询条件
     * @return 符合查询条件的记录
     * @version v1.0
     * @since v1.0
     */
    @Override
    public List<CustomerRuleEntity> findEntitys(Map<String, Object> param) {
        return customerRuleDao.findEntitys(param);
    }

    /**
     * 获取符合查询条件的记录总数
     *
     * @param param 查询条件
     * @return 符合查询条件的记录总数
     * @version v1.0
     * @since v1.0
     */
    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return customerRuleDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    /**
     * 根据entity的ID值判断是新增或是更新
     *
     * @param entity
     * @return
     * @version v1.0
     * @since v1.0
     */
    @Override
    public Integer save(CustomerRuleEntity entity) {
        if (entity.getId() == null || entity.getId().equals(0L)) {
            return customerRuleDao.insert(entity);
        }
        return customerRuleDao.update(entity);
    }

    @Override
    public List<CustomerRuleEntity> getSpecialRules(String corpid) {
        List<Integer> ruleTypeIn = new ArrayList<>();
        ruleTypeIn.add(CustomerRuleEnum.MAX_CUSTOMER_NUM_RULE.getCode());
        ruleTypeIn.add(CustomerRuleEnum.PRE_PRINCIPAL_RULE.getCode());
        ruleTypeIn.add(CustomerRuleEnum.GAIN_RATE_RULE.getCode());
        //数据库查询，获取影响到获取客户判断的三个规则
        Map<String, Object> param = new HashMap<>(ruleTypeIn.size());
        param.put("corpid", corpid);
        param.put("ruleTypeIn", ruleTypeIn);
        param.put("status", 1);
        param.put("del", 0);
        return customerRuleDao.findEntitys(param);
    }

    @Override
    public Integer updateDelByCorpid(Integer del, String corpid) {
        return customerRuleDao.updateDelByCorpid(del,corpid);
    }
}
